package com.fadadianqi.mall.admin.controller;

import com.fadadianqi.commons.PrimaryKeyCreator;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.mall.model.Nav;
import com.fadadianqi.mall.product.model.ItemSPU;
import com.fadadianqi.mall.product.service.ItemSPUService;
import com.fadadianqi.mall.service.NavService;
import com.yeqiangwei.commons.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by aroot on 2/26/14.
 */
@Controller
public class InventoryController {

    @Autowired
    private ItemSPUService itemSPUService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private NavService navService;

    @RequestMapping(value = "/mall/adm/product/inventory")
    public String showList(HttpServletRequest request, ModelMap modelMap) {

        return "/mall/admin/inventory/list";
    }

    @RequestMapping(value = "/mall/adm/product/inventory/create")
    public String showForm(HttpServletRequest request, ModelMap modelMap) {
        String spuId = ParamUtils.getString(request, "spuId", null);
        ItemSPU spu = itemSPUService.getById(spuId);
        if (null != spu) {
            Brand brand = goodsService.getBrandById(spu.getBrandId());
            modelMap.put("brand", brand);
            List<Nav> navList = navService.findBySpuId(spu.getId());
            modelMap.put("navList", navList);
        }
        modelMap.put("spu", spu);
        modelMap.put("id", PrimaryKeyCreator.create()); // 提前生成好主键以便图片等其他附件按ID存储
        return "/mall/admin/inventory/form";
    }
}
